package org.proteored.miapeapi.xml.pride.msi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.proteored.miapeapi.cv.Accession;
import org.proteored.miapeapi.cv.ControlVocabularyManager;
import org.proteored.miapeapi.cv.ControlVocabularyTerm;
import org.proteored.miapeapi.cv.msi.PrideProject;
import org.proteored.miapeapi.interfaces.Software;
import org.proteored.miapeapi.interfaces.msi.AdditionalParameter;
import org.proteored.miapeapi.interfaces.msi.Database;
import org.proteored.miapeapi.interfaces.msi.InputParameter;
import org.proteored.miapeapi.xml.mzidentml.AdditionalParameterImpl;
import org.proteored.miapeapi.xml.pride.autogenerated.ExperimentType;
import org.proteored.miapeapi.xml.pride.autogenerated.GelFreeIdentificationType;
import org.proteored.miapeapi.xml.pride.autogenerated.TwoDimensionalIdentificationType;
import org.proteored.miapeapi.xml.pride.util.Parameter;
import org.proteored.miapeapi.xml.pride.util.Utils;

public class InputParameterImpl implements InputParameter {
	private final ExperimentType experiment;
	private final Integer identifier;
	private final Set<Software> msiSoftwares;
	private final ControlVocabularyManager cvManager;

	public InputParameterImpl(ExperimentType experiment, Set<Software> msiSoftwares,
			Integer identifier, ControlVocabularyManager cvManager) {
		this.experiment = experiment;
		this.identifier = identifier;
		this.msiSoftwares = msiSoftwares;
		this.cvManager = cvManager;
	}

	@Override
	public String getAaModif() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAdditionalCleavages() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCleavageName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCleavageRules() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<Database> getDatabases() {
		Set<Database> result = new HashSet<Database>();
		Map<String, String> databases = new HashMap<String, String>();
		final List<GelFreeIdentificationType> gelFreeIdentification = experiment
				.getGelFreeIdentification();
		if (gelFreeIdentification != null) {
			for (Object object : gelFreeIdentification) {
				GelFreeIdentificationType identification = (GelFreeIdentificationType) object;
				final String database = identification.getDatabase();
				if (database != null && databases.containsKey(database) == false) {
					databases.put(database, identification.getDatabaseVersion());
				}
			}
		}
		final List<TwoDimensionalIdentificationType> twoDimensionalIdentification = experiment
				.getTwoDimensionalIdentification();
		if (twoDimensionalIdentification != null) {
			for (Object object : twoDimensionalIdentification) {
				TwoDimensionalIdentificationType identification = (TwoDimensionalIdentificationType) object;
				final String database = identification.getDatabase();
				if (database != null && databases.containsKey(database) == false) {
					databases.put(database, identification.getDatabaseVersion());
				}
			}
		}
		for (String databaseName : databases.keySet()) {
			result.add(new DatabaseImpl(databaseName, databases.get(databaseName)));
		}
		return result;
	}

	@Override
	public String getFragmentMassTolerance() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getFragmentMassToleranceUnit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getMinScore() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getMisscleavages() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		return "Input Parameters";
	}

	@Override
	public String getNumEntries() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPmfMassTolerance() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPmfMassToleranceUnit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPrecursorMassTolerance() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPrecursorMassToleranceUnit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTaxonomy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<AdditionalParameter> getAdditionalParameters() {
		if (experiment.getAdditional() != null
				&& experiment.getAdditional().getCvParamOrUserParam() != null) {
			// Exclude PRIDEProject
			List<Accession> exclusionList = new ArrayList<Accession>();
			final List<ControlVocabularyTerm> possibleValues = PrideProject.getInstance(cvManager)
					.getPossibleValues();
			if (possibleValues != null) {
				for (ControlVocabularyTerm controlVocabularyTerm : possibleValues) {
					exclusionList.add(controlVocabularyTerm.getTermAccession());
				}
			}
			Set<AdditionalParameter> result = new HashSet<AdditionalParameter>();
			List<Parameter> createListParameter = Utils.createListParameter(experiment
					.getAdditional().getCvParamOrUserParam(), exclusionList);
			if (createListParameter != null) {
				for (Parameter param : createListParameter) {
					result.add(new AdditionalParameterImpl(param.getName(), param.getValue()));
				}
				return result;
			}
		}
		return null;
	}

	@Override
	public String getScoringAlgorithm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getSearchType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getId() {
		return identifier;
	}

	@Override
	public Software getSoftware() {
		if (this.msiSoftwares != null) {
			if (this.msiSoftwares.size() > 0)
				return msiSoftwares.iterator().next();
		}
		return null;
	}

}
